package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.mapper.RoleMapper;
import cn.wolfcode.crm.qo.PageResult;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.IRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class RoleServiceImpl implements IRoleService{
	@Autowired
	private RoleMapper roleMapper;
	@Override
	public void insert(Role role, Long[] permissionIds) {

		roleMapper.insert(role);

		if(permissionIds!=null) {
			for (Long permissionId : permissionIds) {
				roleMapper.insertRelation(role.getId(), permissionId);
			}
		}
	}

	@Override
	public void delete(Long id) {
		roleMapper.deleteRelation(id);
		roleMapper.delete(id);
	}

	@Override
	public void update(Role role, Long[] permissionIds) {
		roleMapper.update(role);
		//先删中间表中该角色的相关数据
		roleMapper.deleteRelation(role.getId());
		if(permissionIds!=null) {
			for (Long permissionId : permissionIds) {
				//如果权限id不为空则从中间表中添加该角色数据
				roleMapper.insertRelation(role.getId(), permissionId);
			}
		}

	}

	@Override
	public Role select(Long id) {
		Role role = roleMapper.select(id);
		return role;
	}

	@Override
	public List<Role> list() {
		List<Role> list = roleMapper.list();
		return list;
	}

	public PageInfo<Role> query(QueryObject qo) {
		PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Role> roles = roleMapper.selectForList(qo);
        return new PageInfo<>(roles);
	}

	public List<String> selectRoleSnByEmployeeId(Long employeeId) {
		return roleMapper.selectRoleSnByEmployeeId(employeeId);
	}


}
